package com.oolatina;

import android.content.Context;
import android.graphics.Color;
import android.widget.FrameLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.view.Display;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.view.View;
import android.view.View.OnClickListener;

public class SCButton implements OnClickListener
{
	Button nButton;
	LinearLayout _layout;
	int _width;
	int _height;
	int _x;
	int _y;
	int ScreenWidth;
	int ScreenHeight;
	int _tag;
	Context _context;
	SCButtonListener listener;
	
	// Delegate Method
	public interface SCButtonListener 
	{
	    public void clickEvent(int tag);
	}
	
	public void init(LinearLayout layout, Context thisContext,SCButtonListener _listener)
    {
		this.listener = _listener;
		_context = thisContext;
		_layout = layout;
		_x = 0;
		_y = 0;
		_width = 0;
		_height = 0;
		
		WindowManager wm = (WindowManager) thisContext.getSystemService(Context.WINDOW_SERVICE);
	    Display display = wm.getDefaultDisplay();
	    ScreenWidth = display.getWidth();
	    ScreenHeight = display.getHeight();
		
		nButton = new Button(thisContext);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.gravity=Gravity.CENTER;
        // Event onClick
        nButton.setOnClickListener(this);
        
        _layout.addView(nButton);
    }
	
	public void SetTag(int tag)
	{
		_tag = tag;
	}
	
	public int GetTag()
	{
		return _tag;
	}
	
	public void SetPosition(int x,int y)
	{
		_x = x;
		_y = y;
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
	}
	
	public void SetSize(int width,int height)
	{
		_width = width;
		_height = height;
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
	}
	
	public void SetPourcentSize(int width, int height)
	{
		float radioWidth = (float)ScreenWidth/100;
    	float radioHeight = (float)ScreenHeight/100;
    	_width = (int)(radioWidth*width);
    	_height = (int)(radioHeight*height);
		
		FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
				_width,_height);
        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
	}
	
	public int SetPourcentWidth(int width)
	{
		float radioWidth = (float)ScreenWidth/100;
		_width = (int)(radioWidth*width);
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				_width,_height);
//        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
        return _width;
	}
	
	public int SetPourcentHeight(int height)
	{
		float radioHeight = (float)ScreenHeight/100;
		_height = (int)(radioHeight*height);
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				_width,_height);
//        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
        return _height;
	}
	
	public void SetHeight(int height)
	{
		_height = height;
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				_width,_height);
//        layoutParams.setMargins(_x,_y,0,0);
        nButton.setLayoutParams(layoutParams);
	}
	
	public void SetBackgroundColor(int alpha,int red,int green,int blue)
    {
		nButton.setBackgroundColor(Color.argb(alpha,red,green,blue));
    }
	
	public void SetLoadIdImage(int idimage)
	{
		nButton.setBackgroundResource(idimage);
	}
	
	@Override
    public void onClick(View v) 
	{
		this.listener.clickEvent(_tag);
    }
}
